package com.opensky.qatest.ui.component;

import org.openqa.selenium.By;

import com.opensky.qatest.ui.page.CuratorPickerPage;
import com.opensky.qatest.ui.page.thirdparty.FacebookLoginPage;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * SignUp form on Home page
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.1
 * 
 */
public class HomePageSignUpForm implements IComponent {

    // ************************ Locators ************************

    private static final String REGFORM = "//div[@class='reg-form']/form";

    private static final String CATEGORIES = "//div[@class='category-follow']";

    private static final String FACEBOOK_LINK = "Log In With Facebook";

    private static final String TERMS_LINK = "terms";

    private static final String NAME_FIELD = "registration_user_firstName";

    private static final String EMAIL_FIELD = "registration_user_email";

    private static final String PASSWORD_FIELD = "registration_user_plainPassword";

    private static final String START_SHOPPING_BTN = "submit";

    private static final String INVALID_EMAIL_ALERT_CONTAINER = "//label[@for='registration_user_email']/span";

    private static final String INVALID_EMAIL_ALERT_TEXT = "Please provide valid email address.";

    private static final String INVALID_PASSWORD_ALERT_CONTAINER = "//label[@for='registration_user_plainPassword']/span";

    private static final String INVALID_PASSWORD_ALERT_TEXT = "Please provide a password.";

    private static final String SHORT_PASSWORD_ALERT_CONTAINER = "//label[@for='registration_user_plainPassword']/span";

    private static final String SHORT_PASSWORD_ALERT_TEXT = "Your password is too short. It must have 6 characters or more.";

    private final WebDriverWrapper driver;

    public HomePageSignUpForm(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    public FacebookLoginPage clickLogInWithFacebookLink() {

	FacebookLoginPage facebookLoginPage = new FacebookLoginPage(driver);
	driver.waitAndClick("FACEBOOK_LINK", By.linkText(FACEBOOK_LINK));
	return facebookLoginPage.isPageLoaded() ? facebookLoginPage : null;
    }

    public CuratorPickerPage signUp(String name, String email, String password) {

	CuratorPickerPage curatorPickerPage = new CuratorPickerPage(driver);

	driver.waitAndSendKeys("NAME_FIELD", By.id(NAME_FIELD), name);
	driver.waitAndSendKeys("EMAIL_FIELD", By.id(EMAIL_FIELD), email);
	driver.waitAndSendKeys("PASSWORD_FIELD", By.id(PASSWORD_FIELD), password);

	driver.waitAndClick("START_SHOPPING_BTN", By.name(START_SHOPPING_BTN));

	if (driver.waitTimeAndDefineElementWithTextPresent("INVALID_EMAIL_ALERT_CONTAINER", By.xpath(INVALID_EMAIL_ALERT_CONTAINER),
		INVALID_EMAIL_ALERT_TEXT, 3)
		|| driver.waitTimeAndDefineElementWithTextPresent("INVALID_PASSWORD_ALERT_CONTAINER",
			By.xpath(INVALID_PASSWORD_ALERT_CONTAINER), INVALID_PASSWORD_ALERT_TEXT, 3)
		|| driver.waitTimeAndDefineElementWithTextPresent("SHORT_PASSWORD_ALERT_CONTAINER",
			By.xpath(SHORT_PASSWORD_ALERT_CONTAINER), SHORT_PASSWORD_ALERT_TEXT, 3)) {
	    return null;
	}

	return curatorPickerPage.isPageLoaded() ? curatorPickerPage : null;
    }

    // ************************ Validations **************************

    @Override
    public boolean isComponentLoaded() {

	return driver.waitAndVerifyElementPresent("REGFORM", By.xpath(REGFORM));
    }

    public boolean isBaseElementsPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("NAME_FIELD", By.id(NAME_FIELD)),
		driver.waitAndVerifyElementPresent("EMAIL_FIELD", By.id(EMAIL_FIELD)),
		driver.waitAndVerifyElementPresent("PASSWORD_FIELD", By.id(PASSWORD_FIELD)),
		driver.waitAndVerifyElementPresent("CATEGORIES", By.xpath(CATEGORIES)),
		driver.waitAndVerifyElementPresent("FACEBOOK_LINK", By.linkText(FACEBOOK_LINK)),
		driver.waitAndVerifyElementPresent("START_SHOPPING_BTN", By.name(START_SHOPPING_BTN)),
		driver.waitAndVerifyElementPresent("TERMS_LINK", By.linkText(TERMS_LINK)));
    }

    public boolean isInvalidEmailAlertPresent() {

	driver.waitAndVerifyElementWithTextPresent("INVALID_EMAIL_ALERT_CONTAINER", By.xpath(INVALID_EMAIL_ALERT_CONTAINER),
		INVALID_EMAIL_ALERT_TEXT);
	return true;
    }

    public boolean isEmptyPasswordAlertPresent() {

	driver.waitAndVerifyElementWithTextPresent("INVALID_PASSWORD_ALERT_CONTAINER", By.xpath(INVALID_PASSWORD_ALERT_CONTAINER),
		INVALID_PASSWORD_ALERT_TEXT);
	return true;
    }

    public boolean isShortPasswordAlertPresent() {

	driver.waitAndVerifyElementWithTextPresent("SHORT_PASSWORD_ALERT_CONTAINER", By.xpath(SHORT_PASSWORD_ALERT_CONTAINER),
		SHORT_PASSWORD_ALERT_TEXT);
	return true;
    }

    @Override
    public String getName() {
	return "Home page signUp form";
    }
}
